package it.polimi.travelDreamEJB.travelpacketmanagement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import it.polimi.travelDreamEJB.basicproductmanagement.BasicProductMgrBean;
import it.polimi.travelDreamEJB.basicproductmanagement.Excursion;
import it.polimi.travelDreamEJB.basicproductmanagement.Hotel;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.ExcursionDTO;
import it.polimi.travelDreamEJB.defaultpacketmanagement.DefaultPacketMgr;
import it.polimi.travelDreamEJB.defaultpacketmanagement.dto.DefaultPacketDTO;
import it.polimi.travelDreamEJB.defaultpacketmanagement.dto.SearchDefaultPacketDTO;
import it.polimi.travelDreamEJB.usermanagement.User;
import it.polimi.travelDreamEJB.usermanagement.UserMgrBean;

@Stateless
public class DefaultPacketMgrBean implements DefaultPacketMgr {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	@Override
	public void save_default_packet(DefaultPacketDTO default_packet) {
		DefaultPacket dp = new DefaultPacket(default_packet);
		List<Excursion> excursions=new ArrayList<Excursion>();
		
		for(ExcursionDTO e : default_packet.getExcursions()){
			excursions.add(new Excursion(e));
		}
		
		User employee = em.find(User.class, default_packet.getEmployee().getEmail());
		dp.setEmployee(employee);
		dp.setHotel(new Hotel(default_packet.getHotel()));
		dp.setExcursions(excursions);
		em.persist(dp);

	}

	@Override
	public void update_default_packet(DefaultPacketDTO default_packet) {
		em.merge(new DefaultPacket(default_packet));
	}

	@Override
	public void delete_default_packet(DefaultPacketDTO default_packet) {
		DefaultPacket dp = em.find(DefaultPacket.class,
				default_packet.getId_default_packet());
		em.remove(dp);
	}

	@Override
	public DefaultPacketDTO getDefaultPacketDTO() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DefaultPacketDTO> findDefaultPacket(
			SearchDefaultPacketDTO searchDefaultPacket) {
		List<DefaultPacketDTO> dpDTOList = new ArrayList<DefaultPacketDTO>();
		Query query = em.createNamedQuery("DefaultPacket.findByDestination", DefaultPacket.class);
		query.setParameter("destination", "%" + searchDefaultPacket.getHotel().getDestination() + "%" );
		query.setParameter("email", "%" + searchDefaultPacket.getUser().getEmail() + "%" );
		query.setParameter("hotelName", "%" + searchDefaultPacket.getHotel().getName() + "%" );
		List<DefaultPacket> dpList = query.getResultList();
		for( DefaultPacket dp : dpList ){
			
			dpDTOList.add(convertDefaultPacketToDTO(dp));
			
		}
		return dpDTOList;
	}
	
	static public DefaultPacketDTO convertDefaultPacketToDTO(DefaultPacket dp){
		
		DefaultPacketDTO dpDTO = new DefaultPacketDTO();
		dpDTO.setId_default_packet(dp.getIdDefaultPacket());
		dpDTO.setEmployee(UserMgrBean.convertToDTO(dp.getEmployee()));
		dpDTO.setHotel(BasicProductMgrBean.convertHotelToDTO(dp.getHotel()));
		List<ExcursionDTO> excursionDTOList = new ArrayList<ExcursionDTO>();
		for( Excursion e : dp.getExcursions() ){
			
			excursionDTOList.add(BasicProductMgrBean.convertExcursionToDTO(e));
			
		}
		dpDTO.setExcursions(excursionDTOList);
		return dpDTO;
		
	}

}
